package com.jade.linkong.service.impl;

import com.jade.common.core.text.Convert;
import com.jade.common.utils.DateUtils;
import com.jade.common.utils.StringUtils;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.LkMatter;
import com.jade.linkong.domain.LkMatterProjectDto;
import com.jade.linkong.mapper.LkMatterMapper;
import com.jade.linkong.mapper.LkMatterProjectMapper;
import com.jade.linkong.mapper.LkMatterTypeMapper;
import com.jade.linkong.service.ILkMatterService;
import com.jade.system.mapper.SysRoleMapper;
import com.jade.system.mapper.SysUserRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 事项模板Service业务层处理
 * 
 * @author jade
 * @date 2020-07-13
 */
@Slf4j
@Service
public class LkMatterServiceImpl implements ILkMatterService 
{
    @Resource
    private LkMatterMapper lkMatterMapper;
    @Resource
    private LkMatterTypeMapper lkMatterTypeMapper;
    @Resource
    private LkMatterProjectMapper projectMapper;
    @Resource
    private SysUserRoleMapper userRoleMapper;
    @Resource
    private SysRoleMapper roleMapper;


    /**
     * 查询事项
     * 
     * @param id 事项ID
     * @return 事项
     */
    @Override
    public LkMatter selectLkMatterById(Long id)
    {
        return lkMatterMapper.selectLkMatterById(id);
    }
 
    /**
     * 查询事项列表
     * 
     * @param lkMatter 事项
     * @return 事项
     */
    @Override
    public List<LkMatter> selectLkMatterList(LkMatter lkMatter)
    {
        List<LkMatter> maList = lkMatterMapper.selectLkMatterList(lkMatter);
        for (LkMatter matter : maList) {
            if (StringUtils.isNotEmpty(matter.getTypeId().toString())) {
                matter.setTypeName(lkMatterTypeMapper.selectLkMatterTypeById(matter.getTypeId()).getTypeName());
            }
        }
        return maList;
    }
 
    /**
     * 新增事项
     * 
     * @param lkMatter 事项
     * @return 结果
     */
    @Override
    public int insertLkMatter(LkMatter lkMatter)
    {
        lkMatter.setCreateTime(DateUtils.getNowDate());
        return lkMatterMapper.insertLkMatter(lkMatter);
    }
 
    /**
     * 修改事项
     * 
     * @param lkMatter 事项
     * @return 结果
     */
    @Override
    public int updateLkMatter(LkMatter lkMatter)
    {
        lkMatter.setUpdateTime(DateUtils.getNowDate());
        return lkMatterMapper.updateLkMatter(lkMatter);
    }
 
    /**
     * 删除事项对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLkMatterByIds(String ids)
    {
        return lkMatterMapper.deleteLkMatterByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除事项信息
     * 
     * @param id 事项ID
     * @return 结果
     */
    @Override
    public int deleteLkMatterById(Long id)
    {
        return lkMatterMapper.deleteLkMatterById(id);
    }
 
    @Override
    public Map<String,List<LkMatter>> selectMattersByIds(String ids,int proId) {
        int userId = ShiroUtils.getUserId().byteValue();
        int count = 0;
        List<LkMatter> matters = new ArrayList<>();
        if(proId != 0){
            count =   projectMapper.selectCountByProjectId(proId);
        }
        if(count > 0){
            log.info("项目有事项");
            String roleKey = ShiroUtils.getSysUser().getRoleKey();
            Long uid = null;
            int acceptingState = 0;
            if ( StringUtils.isNotNull(roleKey) && roleKey.equals("company")){
                uid = ShiroUtils.getSysUser().getUserId();
                acceptingState = 2;
            }else if (StringUtils.isNotNull(roleKey) && roleKey.equals("dept")){
                uid = ShiroUtils.getSysUser().getDeptId();
                acceptingState = 2;
            }
            if(StringUtils.equals("0",ids)){
                    matters = projectMapper.selectListByProjectId2(userId,proId,uid,acceptingState);
            }else{
                    matters = projectMapper.selectListByProjectId(userId,proId,uid,acceptingState);
            }

        }else{
            log.info("项目无事项");
            matters  = lkMatterMapper.selectMattersByIds(Convert.toStrArray(ids));
        }

        Map<String, List<LkMatter>> mmap = matters.stream().collect(Collectors.groupingBy(LkMatter::getTypeName));

        return mmap;
    }

    @Override
    public Map<String,Map<String, List<LkMatterProjectDto>>> findListByProjectId2(long projectId) {

        Map<String,Map<String, List<LkMatterProjectDto>>> map = new HashMap<>();

        List<LkMatterProjectDto> list = projectMapper.findListByProjectId2(projectId);
        // 按父级类型名称分组,
        Map<String, List<LkMatterProjectDto>> mmap = list.stream().collect(Collectors.groupingBy(LkMatterProjectDto::getParentName));
        for (Map.Entry<String, List<LkMatterProjectDto>> entry : mmap.entrySet()) {
                List<LkMatterProjectDto> myList = entry.getValue();
                // 遍历后再分组
                Map<String, List<LkMatterProjectDto>> mmap2 = myList.stream().collect(Collectors.groupingBy(LkMatterProjectDto::getTypeName));
                map.put(entry.getKey(),mmap2);
             }

        return map;
    }


}
